<?php
/**
 * User: xudongsheng
 * Date: 2017/9/11
 * Time: 下午10:16
 */

namespace app\admin\controller;

use think\Request;
use think\Route;
use app\admin\model\Column;
/** 单页模型 控制器
 * Class SinglePage
 * @package app\admin\controller
 */
class SinglePage extends Common
{
    public function lists(Request $request,$column_id){
        ($page = Column::get($column_id) AND $page['type'] != Column::LINKS_TYPE AND $page['model_id'] == Column::SINGLE_PAGE)
        OR $this->error('不存在此栏目或此栏目无法展示！');

        $request->isPost() AND $this->saveContent($page);
        // 查 当前 分类的 面包屑  求所有父级分类
        $this->assign('parents',Column::init()->crumbs($column_id));
        // 子栏目
        $childColumn = Column::init()->classAChild($column_id);
        $this->assign('childColumn',$childColumn);
        $this->assign('page',$page);
        return $this->fetch();
    }

    private function saveContent($page){
        $error = $this->validate(input('post.'),[
            'description|摘要'       =>  'require|max:250|min:1|token',
            'content|内容'           =>  'require|max:2000'
        ]);
        if ($error !== true){
            $this->error($error);
        }
//        !($page =  Column::get($column_id)) AND $this->error('此单页不存在无法更新！');
        $page->description = input('post.description');
        $page->content = input('post.content');
        if ($page->save()){
            $this->success('更新成功！',url('/admin/single_page/lists',['column_id'=>$page->column_id]));
        }
        $this->error('更新失败!');

    }
}